package com.example.qrcodecamera;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private Spinner roleSpinner;
    private Button loginButton;
    private String selectedRole = "用户";
    
    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        // 初始化数据库帮助类
        dbHelper = new DatabaseHelper(this);

        // 初始化视图
        usernameEditText = findViewById(R.id.username_edit_text);
        passwordEditText = findViewById(R.id.password_edit_text);
        roleSpinner = findViewById(R.id.role_spinner);
        loginButton = findViewById(R.id.login_button);

        // 设置角色选择下拉框
        setupRoleSpinner();
        
        // 初始化默认账号
        AccountManager.initializeDefaultAccounts(dbHelper);

        // 设置登录按钮点击事件
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptLogin();
            }
        });
    }

    private void setupRoleSpinner() {
        // 创建角色选择适配器
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.roles_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        roleSpinner.setAdapter(adapter);

        // 设置选择监听器
        roleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedRole = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                selectedRole = "用户";
            }
        });
    }

    private void attemptLogin() {
        // 获取输入内容
        String username = usernameEditText.getText().toString().trim();
        String password = passwordEditText.getText().toString().trim();

        // 简单的输入验证
        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 登录验证
        if (isValidCredentials(username, password)) {
            // 根据角色跳转到不同的页面
            if ("超级管理员".equals(selectedRole)) {
                Intent intent = new Intent(LoginActivity.this, AccountManagementActivity.class);
                startActivity(intent);
            } else if ("管理员".equals(selectedRole)) {
                Intent intent = new Intent(LoginActivity.this, MidadminActivity.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(LoginActivity.this, MIduserActivity.class);
                startActivity(intent);
            }
            finish(); // 结束登录页面
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean isValidCredentials(String username, String password) {
        if ("超级管理员".equals(selectedRole)) {
            return AccountManager.validateSuperAdmin(username, password);
        } else if ("管理员".equals(selectedRole)) {
            return dbHelper.checkAdminLogin(username, password);
        } else {
            return dbHelper.checkUserLogin(username, password);
        }
    }
    
    // 静态方法用于获取当前的用户名和密码 - 为了兼容性保留
    public static String getUserUsername() {
        return "123";
    }
    
    public static String getUserPassword() {
        return "123";
    }
    
    public static String getAdminUsername() {
        return "123";
    }
    
    public static String getAdminPassword() {
        return "123";
    }
} 